package com.hb.core.config;

import cn.hutool.core.util.StrUtil;
import com.p6spy.engine.common.P6Util;
import com.p6spy.engine.spy.appender.MessageFormattingStrategy;

import java.text.MessageFormat;

/**
 * 打印p6spy日志
 *
 * @author zhaojk
 */
public class AdminP6SpyLogger implements MessageFormattingStrategy {

    /**
     * 格式化sql语句输出
     *
     * @param connectionId 数据库链接ID
     * @param now          当前时间
     * @param elapsed      语句执行时间
     * @param category
     * @param prepared
     * @param sql          执行语句
     * @param url          数据库链接URL
     * @return
     */
    @Override
    public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) {
        if (StrUtil.isEmpty(sql)) {
            return StrUtil.EMPTY;
        }
        String logTpl = "执行时间：{0}ms\n{1}\n";
        if (StrUtil.length(sql) > 3000) {
            return MessageFormat.format(logTpl, elapsed, prepared.replaceAll("[\\s]+", " "));
        }
        return MessageFormat.format(logTpl, elapsed, sql.replaceAll("[\\s]+", " "));
    }

}
